﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Demo_G.O.S.E.ServerEngine.Common;
using System.Threading;

namespace Demo_G.O.S.E.ServerEngine.Mobile
{
    /// <summary>
    /// 
    /// </summary>
    public class AggressorInfo : IRelease
    {
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private BaseMobile m_Attacker = null;
        /// <summary>
        /// 
        /// </summary>
        private BaseMobile m_Defender = null;
        /// <summary>
        /// 
        /// </summary>
        private DateTime m_LastCombatTime = DateTime.Now;
        #endregion

        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 
        /// </summary>
        /// <param name="attacker"></param>
        /// <param name="defender"></param>
        /// <param name="criminal"></param>
        private AggressorInfo( BaseMobile attacker, BaseMobile defender )
        {
            m_Attacker = attacker;
            m_Defender = defender;

            Refresh();
        }
        #endregion

        #region zh-CHS 共有属性 | en Public Properties
        /// <summary>
        /// 
        /// </summary>
        public BaseMobile Attacker
        {
            get { return m_Attacker; }
        }

        /// <summary>
        /// 
        /// </summary>
        public BaseMobile Defender
        {
            get { return m_Defender; }
        }

        /// <summary>
        /// 
        /// </summary>
        public DateTime LastCombatTime
        {
            get { return m_LastCombatTime; }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool Expired
        {
            get { return ( m_Attacker.Deleted || m_Defender.Deleted || DateTime.Now >= ( m_LastCombatTime + s_ExpireDelay ) ); }
        }
        #endregion

        #region zh-CHS 共有静态属性 | en Public Static Properties
        #region zh-CHS 私有静态成员变量 | en Private Static Member Variables
        /// <summary>
        /// 
        /// </summary>
        private static TimeSpan s_ExpireDelay = TimeSpan.FromMinutes( 2.0 );
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public static TimeSpan ExpireDelay
        {
            get { return s_ExpireDelay; }
            set { s_ExpireDelay = value; }
        }
        #endregion

        #region zh-CHS 共有方法 | en Public Methods
        /// <summary>
        /// 
        /// </summary>
        public void Refresh()
        {
            m_LastCombatTime = DateTime.Now;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Release()
        {
            Monitor.Enter( s_LockAggressorInfoPool );
            {
                s_AggressorInfoPool.Enqueue( this );
            }
            Monitor.Exit( s_LockAggressorInfoPool );
        }
        #endregion

        #region zh-CHS 共有静态方法 | en Public Static Methods
        #region zh-CHS 私有静态成员变量 | en Private Static Member Variables
        /// <summary>
        /// 
        /// </summary>
        private static Queue<AggressorInfo> s_AggressorInfoPool = new Queue<AggressorInfo>();
        /// <summary>
        /// 锁
        /// </summary>
        private static object s_LockAggressorInfoPool = new object();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="attacker"></param>
        /// <param name="defender"></param>
        /// <param name="criminal"></param>
        /// <returns></returns>
        public static AggressorInfo Instance( BaseMobile attacker, BaseMobile defender )
        {
            AggressorInfo l_AggressorInfo = null;

            Monitor.Enter( s_LockAggressorInfoPool );
            {
                if ( s_AggressorInfoPool.Count > 0 )
                {
                    l_AggressorInfo = s_AggressorInfoPool.Dequeue();

                    l_AggressorInfo.m_Attacker = attacker;
                    l_AggressorInfo.m_Defender = defender;

                    l_AggressorInfo.Refresh();
                }
            }
            Monitor.Exit( s_LockAggressorInfoPool );

            if ( l_AggressorInfo == null )
                l_AggressorInfo = new AggressorInfo( attacker, defender );

            return l_AggressorInfo;
        }
        #endregion
    }
}

